package com.tengju.bff.interfaces.manage.crm.user;

import com.tengju.bff.interfaces.manage.crm.user.facade.UserManageFacade;
import com.tengju.bff.interfaces.manage.crm.user.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.application.user.dto.MobileManageChangeForm;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.JsonUtil;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Validated
@RestController
@Api(tags = "用户后台管理")
@RequestMapping(value = "manage/crm/user")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ ={@Autowired})
public class CrmUserManageController {

    private final UserManageFacade userManageFacade;

    private final UserInfoRepository userInfoRepository;

    @RequestMapping(value = "/change/mobile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> changeMobile(@Valid @RequestBody MobileManageChangeForm mobileManageChangeForm) {

        try {
            userManageFacade.changeMobile(mobileManageChangeForm);
            return ApiResponse.newSuccess(true);
        } catch (DomainException | ApplicationException | InterfaceException e) {
            log.error("手机号:{},手机号变更失败：", mobileManageChangeForm.getOldMobile(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "/queryByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PageUserVo> queryUser(UserForm form) {
        try {
            form.setOperator(ServletContextUtil.getServletContext().getRealName());
            return ApiResponse.newSuccess(userManageFacade.queryUser(form));
        } catch (DomainException | ApplicationException e) {
            log.error("用户查询失败,form：{}", JsonUtil.toJSONString(form), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<UserDetailVO> detail(@NotNull(message = "用户idCode不能为空") Long idCode) {
        try {
            return ApiResponse.newSuccess(userManageFacade.getDetail(new UserIdCode(idCode)));
        } catch (DomainException | ApplicationException e) {
            log.error("查询用户基本资料失败,idCode：{}", idCode, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/starIndicator", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<StarIndicatorVO> starIndicator(@NotNull(message = "用户idCode不能为空") Long idCode) {
        try {
            return ApiResponse.newSuccess(userManageFacade.getStarIndicator(new UserIdCode(idCode)));
        } catch (DomainException | ApplicationException e) {
            log.error("晋升指标查询失败,idCode：{}", idCode, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/queryById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<UserVo> queryById(@NotNull(message = "用户idCode不能为空") @RequestParam("idCode") Long idCode) {
        try {
            return ApiResponse.newSuccess(userManageFacade.queryById(new UserIdCode(idCode)));
        } catch (DomainException | ApplicationException e) {
            log.error("用户查询失败,idCode：{}", idCode, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "更改服务商姓名", notes = "更改服务商姓名")
    @RequestMapping(value = "/changeUsername", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Void> changeUsername(@RequestBody ChangeUsernameForm changeUsernameForm) {
        userInfoRepository.updateRealName(new UserIdCode(changeUsernameForm.getIdCode()), changeUsernameForm.getName());
        return ApiResponse.newSuccess();
    }

}
